package manage

import (
	"fmt"
	"github.com/gin-gonic/gin"
	logic2 "iot-base/auth/admin/logic"
	"iot-base/auth/api/logic"
	"iot-base/auth/logger"
	"iot-base/auth/svc"
	"iot-base/auth/types"
)

func QueryThingList(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	var req types.ReqMgGetThing
	err := c.ShouldBind(&req)
	if err != nil {
		logger.Log.Errorf("QueryThingList [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}

	// 判断用户有没有权限
	userLogic := logic.NewUserLogic()
	isAuth := userLogic.CheckThingAuth(uid.(int64), req.StationId)
	if isAuth == false {
		resp.Success(gin.H{})
		return
	}
	total, _ := svc.Dao.TThings.QueryMgThingCount(req.StationId)
	result, _ := svc.Dao.TThings.QueryMgThingList(req.StationId, req.Page, req.Size)
	resp.Success(gin.H{"total": total, "list": result})
}

func GetThingSelect(c *gin.Context) {
	resp := types.NewResult(c)
	modelIds, _ := svc.Dao.TTModel.GetAllRules()
	var edges = make([]types.RespSelectEdge, 2)
	_ = svc.Dao.TThings.QuerySelectEdge("IOTBASEmEdgeCTL", &edges)
	//ids := make([]string, 0, len(modelIds))
	//for _, v := range modelIds {
	//	ids = append(ids, v.ModelId)
	//}
	//// 查询协议
	//protocolList, err := svc.Dao.TProtocol.GetProtocolByModelIds(ids)
	//protocolMap := make(map[string][]interface{})
	//if err == nil {
	//	// 协议归类
	//	for i := range protocolList {
	//		id := protocolList[i].ModelID
	//		protocolMap[id] = append(protocolMap[id], protocolList[i])
	//	}
	//}
	//for i := range modelIds {
	//	if v, ok := protocolMap[modelIds[i].ModelId]; ok {
	//		modelIds[i].Protocol = v
	//	} else {
	//		modelIds[i].Protocol = []interface{}{}
	//	}
	//}
	resp.Success(gin.H{"modelList": modelIds, "edgeList": edges})
}

func GetModelProtocol(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	var req types.ReqModelProtocol
	err := c.ShouldBind(&req)
	if err != nil {
		logger.Log.Errorf("GetModelProtocol [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	protocolList, err := svc.Dao.TProtocol.GetProtocolByModelIds(req.ModelId)
	if err != nil {
		logger.Log.Errorf("[%v] GetProtocolByModelIds err:=%v", requestId, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
	}
	resp.Success(gin.H{"list": protocolList})
}

func GetThingParam(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	var req types.ReqGetThingParam
	err := c.ShouldBind(&req)
	if err != nil {
		logger.Log.Errorf("GetThingParam [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	// 获取参数设备参数
	result, err := logic2.NewThingLogic().GetThingParma(req.ThingId, req.ModelId)
	if err != nil {
		logger.Log.Errorf("[%s] GetThingParma err:=%v", requestId, err)
		resp.ErrorCode(types.ExceptionError, err.Error())
		return
	}
	// 获取物模型关联的协议 spec   properties
	protocolList, err := svc.Dao.TProtocol.GetProtocolByModelIds(req.ModelId)
	if err != nil {
		logger.Log.Errorf("[%v] GetProtocolByModelIds err:=%v", requestId, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
	}
	resp.Success(gin.H{"params": result, "protocolList": protocolList})
}

func AddThing(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	var req types.ReqMgAddThing
	err := c.ShouldBind(&req)
	if err != nil {
		logger.Log.Errorf("AddThing [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		fmt.Println(err)
		return
	}
	err = logic2.NewThingLogic().AddThing(req)
	if err != nil {
		logger.Log.Errorf("AddThing [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}
	resp.Success(gin.H{})
	return
}

func DelThing(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	var req types.ReqDelThing
	err := c.ShouldBind(&req)
	if err != nil {
		logger.Log.Errorf("DelThing [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		fmt.Println(err)
		return
	}
	err = logic2.NewThingLogic().DelThing(req)
	if err != nil {
		logger.Log.Errorf("AddThing [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}

	resp.Success(gin.H{})
	return
}

func ModifyThing(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	var req types.ReqMgModifyThing
	err := c.ShouldBind(&req)
	if err != nil {
		logger.Log.Errorf("ModifyThing [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.ParamsError, "")
		fmt.Println(err)
		return
	}
	err = logic2.NewThingLogic().ModifyThing(req)
	if err != nil {
		logger.Log.Errorf("ModifyThing [%s] uid = [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}
	resp.Success(gin.H{})
	return
}
